﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    public partial class Warehouse : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        //private Dictionary<string, object> dicQueryParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        WarehouseData m_Data;
        public DataSet dsParamaters;
        private string frominspectiondate = "", endinspectiondate = "";
        
        #endregion
        public Warehouse()
        {
            InitializeComponent();
            this.Name = "仓库报表";

        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
           

            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new WarehouseData(dicParamater);
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
        }

        private void InitDataBinding()
        {
            try
            { 
                this.setUI.Tables.Clear(); 
                setUI.AcceptChanges();

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 

        private void Yarn_Inventory_Click(object sender, RoutedEventArgs e)
        {
            this.Yarn_Inventory.Visibility = Visibility.Collapsed;
            InitDataBinding();
            DataSet dsResult = new DataSet();
            dsResult.Clear();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT CODE,GLLB,ART,VAR,BEZ,SUPPLIER,SUPPLIERBATCH,LOT,SUM(WEI) AS WEI "
                    + " FROM INVENTORY_YARN_REPORT_LB GROUP BY CODE,GLLB,ART,VAR,BEZ,SUPPLIER,SUPPLIERBATCH,LOT "
                                ;
                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "YARN_INVENTORY";
                }
                byte[] b;
                string value;
                foreach (DataRow dw in dsResult.Tables["YARN_INVENTORY"].Rows)
                {
                    foreach (DataColumn dc in dsResult.Tables["YARN_INVENTORY"].Columns)
                    {
                        if (dc.ColumnName == "BEZ")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }
                        if (dc.ColumnName == "SUPPLIER")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }

                    }
                }

                WarehouseReport m_rpt = new WarehouseReport(dicParamater);
                m_rpt.dsSource = dsResult;
                m_rpt.YARN_INVENTORY_TO_EXCEL("EXCEL");

                this.Yarn_Inventory.Visibility = Visibility.Visible;

            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }


        }

        private void Fabric_Inventory_Click(object sender, RoutedEventArgs e)
        {
            this.Fabric_Inventory.Visibility = Visibility.Collapsed;
            InitDataBinding();
            DataSet dsResult = new DataSet();
            dsResult.Clear();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT * FROM FINISH_INVENTORY_SUMMARY "
                                ;
                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "FINISH_INVENTORY_SUMMARY";
                }

                WarehouseReport m_rpt = new WarehouseReport(dicParamater);
                m_rpt.dsSource = dsResult;
                m_rpt.exAgent = m_Data.exAgent;
                m_rpt.FINISH_INVENTORY_SUMMARY_TO_EXCEL("EXCEL"); 
                this.Fabric_Inventory.Visibility = Visibility.Visible;
                m_Data = new WarehouseData(dicParamater);
                m_Data.OracleDB = this.OracleDB;
                m_Data.proxy = this.proxy;


            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }


        }
        private void FROM_INSPECTION_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                frominspectiondate = "";
                frominspectiondate =m_Data.GetDate(sender);

            }

            catch (Exception ex)
            {

                Tracer.Error(ex.StackTrace, ex);

            }

        }

        private void END_INSPECTION_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                endinspectiondate = "";
                endinspectiondate = m_Data.GetDate(sender); 
            }

            catch (Exception ex)
            { 
                Tracer.Error(ex.StackTrace, ex); 
            }
        }
        private void Piece_Inspection_Click(object sender, RoutedEventArgs e)
        {
            this.Piece_Inspection.Visibility = Visibility.Collapsed;
            InitDataBinding();
            DataSet dsResult = new DataSet();
            dsResult.Clear();

            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT C.WBBDPART,A.WBFRWKRS||A.WBFRWLFD||A.WBFRWPOS AS CARDNO, "
                    + " C.WBBSTKNR,A.WBFVERSION,B.WBFKNEUDAT,B.WBFKWARSCH, "
                    + " B.WBFKSART,ROUND(C.WBBSSTKLG/1000/0.9144) AS YARDQTY,A.WBFFSCHL,D.DES,ROUND(A.WBFLGVON/1000/0.9144) AS LENFROM, "
                    + " ROUND(A.WBFLGBIS/1000/0.9144) AS LENUPTO,ROUND(A.WBFBRTVON/1000) AS WIDTHFROM, "
                    + " ROUND(A.WBFBRTBIS/1000) AS WIDTHUPTO ,'' AS REMARK "
                    + " FROM WBFEHL A,WBFKOPF B,WBBST C, "
                    + " (select substr(sdkey,1,2) AS MANDANT,substr(sdkey,3,3) CODE, "
                    + " SUBSTR(SDDATEN,2,8) AS DES from sddat where sdsa = '0267'and substr(sddaten,1,1)='A' ) D "
                    + " WHERE A.WBFMANDANT = B.WBFKMANDANT AND A.WBFRWKRS = B.WBFKRWKRS "
                    + " AND A.WBFRWLFD = B.WBFKRWLFD AND A.WBFRWPOS = B.WBFKRWPOS "
                    + " AND A.WBFVERSION = B.WBFKVERSION AND A.WBFMANDANT = C.WBBMANDANT "
                    + " AND A.WBFRWKRS = C.WBBRWKRS AND A.WBFRWLFD = C.WBBRWLFD "
                    + " AND A.WBFRWPOS = C.WBBPOS  AND A.WBFMANDANT = D.MANDANT "
                    + " AND A.WBFFSCHL = D.CODE "
                    + " AND (B.WBFKNEUDAT >= :FROMINSPECTIONDATE OR :FROMINSPECTIONDATE IS NULL) "
                    + " AND (B.WBFKNEUDAT <= :ENDINSPECTIONDATE OR :ENDINSPECTIONDATE IS NULL) "
                    + " AND (trim(C.WBBDPART) = :LOT OR :LOT IS NULL ) "
                    ;
                orlReadCommand.Parameters.Add("FROMINSPECTIONDATE", OracleType.NVarChar).Value = frominspectiondate;
                orlReadCommand.Parameters.Add("ENDINSPECTIONDATE", OracleType.NVarChar).Value = endinspectiondate;
                orlReadCommand.Parameters.Add("LOT", OracleType.VarChar).Value = this.LOT.Text.Trim();
                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "PIECE_INSPECTION";
                }
                byte[] b;
                string value;
                foreach (DataRow dw in dsResult.Tables["PIECE_INSPECTION"].Rows)
                {
                    foreach (DataColumn dc in dsResult.Tables["PIECE_INSPECTION"].Columns)
                    {
                        if (dc.ColumnName == "DES")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }
                    }
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    setUI = m_Data.ImportDataTable(setUI, dtTemp);
                    DataTable tableWBFEHL = setUI.Tables["PIECE_INSPECTION"];
                    foreach (DataRow rowWBFEHL in tableWBFEHL.Rows)
                    {
                        string CARDNO = rowWBFEHL["CARDNO"].ToString() + rowWBFEHL["WBFVERSION"].ToString();
                        rowWBFEHL["REMARK"] = m_Data.GetCommentsFromTXTIND("WBF", CARDNO);

                    }
                }

                WarehouseReport m_rpt = new WarehouseReport(dicParamater);
                m_rpt.dsSource = setUI;
                m_rpt.Piece_Inspection_TO_EXCEL("EXCEL");

                this.Piece_Inspection.Visibility = Visibility.Visible;
                this.FROM_INSPECTION_DATE.Text = "";
                this.END_INSPECTION_DATE.Text = "";
                this.LOT.Text = null;
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            } 

        } 

    }
}
